﻿using AutoMapper;
using Management.Infrastructure.Interfaces.BOMManagement;
using MediatR;
using Management.Errorcode.ErrorCode;
using Management.API.Application.Controller.Management.Product;
using Management.API.DTOs.Management;


namespace Management.API.Application.CommandHandler.Management.Product
{
    public class GetProductClassificationCommandHandler : IRequestHandler<GetProductClassificationCommand, ApiResult<IList<GetProductClassificationDTO>>>
    {
        private readonly IProductClassificationRepository iProductClassificationRepository;
        private readonly IMapper _mapper;
        private readonly ILogger<GetProductClassificationCommandHandler> _logger;

        public GetProductClassificationCommandHandler(IProductClassificationRepository IProductClassificationRepository,
             IMapper mapper, ILogger<GetProductClassificationCommandHandler> logger)
        {
            iProductClassificationRepository = IProductClassificationRepository;
            _mapper = mapper;
            _logger = logger;
        }

        public async Task<ApiResult<IList<GetProductClassificationDTO>>> Handle(GetProductClassificationCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var useList = await iProductClassificationRepository.GetAllAsync();
                var useLists = _mapper.Map<IList<GetProductClassificationDTO>>(useList);
                return ApiResult<IList<GetProductClassificationDTO>>.Success(useLists, StatusCodeEnum.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "GetProductClassificationCommandHandler");
                throw;
            }

        }




    }
}
